<template lang="pug">
.app-container
  // 搜索栏
  el-form(:model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch")
    el-form-item(label="昵称")
      el-input(
        v-model="queryParams.nickName"
        placeholder="请输入用户昵称"
        clearable
        style="..."
        @keyup.enter.native="handleQuery"
      )
    el-form-item(label="状态" prop="status")
      el-select(
        v-model="queryParams.status"
        placeholder="请选择状态"
        clearable
        style="..."
      )
        el-option(
          v-for="dict in dict.type.sys_normal_disable"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        )
    el-form-item
      el-button(type="primary" icon="el-icon-search" size="mini" @click="handleQuery") 搜索
      el-button(icon="el-icon-refresh" size="mini" @click="resetQuery") 重置

  // 工具栏按钮
  el-row.mb8(:gutter="10")
    el-col(:span="1.5")
      el-button(
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
      ) 新增
    el-col(:span="1.5")
      el-button(
        type="success"
        plain
        icon="el-icon-edit"
        size="mini"
        :disabled="single"
        @click="handleUpdate"
      ) 修改
    el-col(:span="1.5")
      el-button(
        type="danger"
        plain
        icon="el-icon-delete"
        size="mini"
        :disabled="multiple"
        @click="handleDelete"
      ) 删除
    el-col(:span="1.5")
      el-button(
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
      ) 导出

  // 表格
  el-table(v-loading="loading" :data="list" @selection-change="handleSelectionChange")
    el-table-column(type="selection" width="55" align="center")
    el-table-column(prop="userId" v-if="false")
    el-table-column(label="用户昵称" align="center" prop="nickName")
    el-table-column(label="用户头像" align="center" prop="pic")
    el-table-column(label="状态" align="center" prop="status")
      template(slot-scope="scope")
        dict-tag(:options="dict.type.sys_normal_disable" :value="scope.row.status")
    el-table-column(label="注册时间" align="center" prop="userRegtime")
    el-table-column(label="操作" align="center" class-name="small-padding fixed-width")
      template(slot-scope="scope")
        el-button(
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
          v-hasPermi="['system:dict:edit']"
        ) 修改
        el-button(
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
          v-hasPermi="['system:dict:remove']"
        ) 删除

  pagination(
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"
  )

  el-dialog(:title="title" :visible.sync="open" width="500px" append-to-body)
    el-form(ref="form" :model="form" :rules="rules" label-width="80px")
      el-form-item(label="用户头像" prop="pic")
        // el-input(v-model="form.pic" placeholder="请输入")
      el-form-item(label="用户昵称" prop="nickName")
        el-input(v-model="form.nickName" placeholder="请输入用户昵称")
      el-form-item(label="状态" size="mini" prop="status")
        el-radio-group(v-model="form.status")
          el-radio(
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.value"
          ) {{ dict.label }}
    .dialog-footer(slot="footer")
      el-button(type="primary" @click="submitForm") 确 定
      el-button(@click="cancel") 取 消
</template>

<script>
import { listUser, addUser, updateUser, delUser, getUser } from "@/api/modules/yami/admin/user";
export default {
  name: 'User',
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        status: undefined,
      },
      // 显示搜索条件
      showSearch: true,
      // 表格数据
      list: [],
      // 总条数
      total: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        nickName: undefined,
        pic: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商城用户"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getUser(userId).then((res) =>
      {
        this.form = res.data;
        this.open = true;
        this.title = "修改商城用户";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$model.msgSuccess('删除成功');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$model.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$model.msgSuccess('添加成功');
              this.open = false;
              this.getList();
            });
          }
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  }
}
</script>

<!--<div class="app-container">-->
<!--&lt;!&ndash; 搜索栏 &ndash;&gt;-->
<!--<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">-->
<!--  <el-form-item label="昵称">-->
<!--    <el-input-->
<!--      v-model="queryParams.nickName"-->
<!--      placeholder="请输入用户昵称"-->
<!--      clearable-->
<!--      style="..."-->
<!--      @keyup.enter.native="handleQuery"-->
<!--    />-->
<!--  </el-form-item>-->
<!--  <el-form-item label="状态" prop="status">-->
<!--    <el-select-->
<!--      v-model="queryParams.status"-->
<!--      placeholder="请选择状态"-->
<!--      clearable-->
<!--      style="..."-->
<!--    >-->
<!--      <el-option-->
<!--        v-for="dict in dict.type.sys_normal_disable"-->
<!--        :key="dict.value"-->
<!--        :label="dict.label"-->
<!--        :value="dict.value"-->
<!--      />-->
<!--    </el-select>-->
<!--  </el-form-item>-->
<!--  <el-form-item>-->
<!--    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--  </el-form-item>-->
<!--</el-form>-->

<!--&lt;!&ndash; 工具栏按钮 &ndash;&gt;-->
<!--<el-row :gutter="10" class="mb8">-->
<!--  <el-col :span="1.5">-->
<!--    <el-button-->
<!--      type="primary"-->
<!--      plain-->
<!--      icon="el-icon-plus"-->
<!--      size="mini"-->
<!--      @click="handleAdd"-->
<!--    >新增</el-button>-->
<!--  </el-col>-->
<!--  <el-col :span="1.5">-->
<!--    <el-button-->
<!--      type="success"-->
<!--      plain-->
<!--      icon="el-icon-edit"-->
<!--      size="mini"-->
<!--      :disabled="single"-->
<!--      @click="handleUpdate"-->
<!--    >修改</el-button>-->
<!--  </el-col>-->
<!--  <el-col :span="1.5">-->
<!--    <el-button-->
<!--      type="danger"-->
<!--      plain-->
<!--      icon="el-icon-delete"-->
<!--      size="mini"-->
<!--      :disabled="multiple"-->
<!--      @click="handleDelete"-->
<!--    >删除</el-button>-->
<!--  </el-col>-->
<!--  <el-col :span="1.5">-->
<!--    <el-button-->
<!--      type="warning"-->
<!--      plain-->
<!--      icon="el-icon-download"-->
<!--      size="mini"-->
<!--      @click="handleExport"-->
<!--    >导出</el-button>-->
<!--  </el-col>-->
<!--</el-row>-->

<!--&lt;!&ndash; 表格 &ndash;&gt;-->
<!--<el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">-->
<!--  <el-table-column type="selection" width="55" align="center" />-->
<!--  <el-table-column prop="userId" v-if="false"></el-table-column>-->
<!--  <el-table-column label="用户昵称" align="center" prop="nickName" />-->
<!--  <el-table-column label="用户头像" align="center" prop="pic" />-->
<!--  <el-table-column label="状态" align="center" prop="status">-->
<!--    <template slot-scope="scope">-->
<!--      <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />-->
<!--    </template>-->
<!--  </el-table-column>-->
<!--  <el-table-column label="注册时间" align="center" prop="userRegtime"></el-table-column>-->
<!--  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--    <el-button-->
<!--      size="mini"-->
<!--      type="text"-->
<!--      icon="el-icon-edit"-->
<!--      @click="handleUpdate(scope.row)"-->
<!--      v-hasPermi="['system:dict:edit']"-->
<!--    >修改</el-button>-->
<!--    <el-button-->
<!--      size="mini"-->
<!--      type="text"-->
<!--      icon="el-icon-delete"-->
<!--      @click="handleDelete(scope.row)"-->
<!--      v-hasPermi="['system:dict:remove']"-->
<!--    >删除</el-button>-->
<!--  </el-table-column>-->
<!--</el-table>-->

<!--<pagination-->
<!--  v-show="total>0"-->
<!--  :total="total"-->
<!--  :page.sync="queryParams.pageNum"-->
<!--  :limit.sync="queryParams.pageSize"-->
<!--  @pagination="getList"-->
<!--/>-->

<!--<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--  <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--    <el-form-item label="用户头像" prop="pic">-->
<!--      &lt;!&ndash; <el-input v-model="form.pic" placeholder="请输入" /> &ndash;&gt;-->
<!--    </el-form-item>-->
<!--    <el-form-item label="用户昵称" prop="nickName">-->
<!--      <el-input v-model="form.nickName" placeholder="请输入用户昵称" />-->
<!--    </el-form-item>-->
<!--    <el-form-item label="状态" size="mini" prop="status">-->
<!--      <el-radio-->
<!--        v-for="dict in dict.type.sys_normal_disable"-->
<!--        :key="dict.value"-->
<!--        :label="dict.value"-->
<!--      >{{ dict.label }}</el-radio>-->
<!--    </el-form-item>-->
<!--  </el-form>-->
<!--  <div slot="footer" class="dialog-footer">-->
<!--    <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--    <el-button @click="cancel">取 消</el-button>-->
<!--  </div>-->
<!--</el-dialog>-->
<!--</div>-->
